package com.serviceprovider.business.module.admin.user.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.role.model.Role;
import com.serviceprovider.business.module.admin.role.model.RoleHierarchy;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IReloadableRoleHierarchy;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleHierarchyService;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.utility.AppUtil;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class RoleHierarchyController {
    @Autowired
    ApplicationConfigProperties applicationConfigProperties;
    @Autowired
    IReloadableRoleHierarchy    roleHierarchy;
    @Autowired
    IRoleHierarchyService       roleHierarchyService;
    @Autowired
    IRoleService                roleService;

    public Map getRolesMap() {
        List<Role> roles = roleService.findAll();
        Map        ret   = new HashMap();

        for (Role r : roles) {
            ret.put(r.getId(), r.getName());
        }

        return ret;
    }

    public List getSignList() {
        List ret = new ArrayList();

        ret.add(">");
        ret.add("<");

        return ret;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.CREATE_ROLE_HIERARCHY,
        method = RequestMethod.GET
    )
    public ModelAndView enterCreateRole(HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.UserRequests.CREATE_ROLE_HIERARCHY_VIEW);

        mav.addObject("roleHierarchy", new RoleHierarchy());
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("signList", getSignList());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.CREATE_ROLE_HIERARCHY,
        method = RequestMethod.POST
    )
    public ModelAndView createRole(@ModelAttribute("roleHierarchy") RoleHierarchy form) {
        ModelAndView        mav       = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLE_HIERARCHY);
        List<RoleHierarchy> userRoles = roleHierarchyService.findAll();

        for (int userCount = 0; userCount < userRoles.size(); userCount++) {
            if (((userRoles.get(userCount).getRole1()) == (form.getRole1()))
                    && ((userRoles.get(userCount).getRole2()) == (form.getRole2()))) {
                mav = new ModelAndView(RequestConstants.UserRequests.VIEW_CREATE_ROLE_HIERARCHY);
                mav.addObject("roleHierarchy", new RoleHierarchy());
                mav.addObject("roleList", roleService.findAll());
                mav.addObject("signList", getSignList());

                return mav;
            }
        }

        roleHierarchyService.insertRoleHierarchy(form);
        roleHierarchy.reloadHierarchy();

        return mav;
    }

    @RequestMapping(value = RequestConstants.UserRequests.VIEW_ROLE_HIERARCHY)
    public ModelAndView viewRoles(HttpServletRequest request, HttpSession sess) {
        ModelAndView mav        = new ModelAndView(RequestConstants.UserRequests.VIEW_ROLE_HIERARCHY_VIEW);
        int          recordSize = applicationConfigProperties.getRecordSize("view_roleHierarchy.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "id"
                            : request.getParameter("sortColumn");

        mav.addObject("roleHierarchyList", roleHierarchyService.findAll(pageNo, recordSize, sortColumn, sortOrder));
        mav.addObject("rolesMap", this.getRolesMap());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.EDIT_ROLE_HIERARCHY,
        method = RequestMethod.GET
    )
    public ModelAndView enterEditRole(HttpServletRequest req, HttpSession sess) {
        ModelAndView mav = new ModelAndView("admin/role/edit_roleHierarchy");

        mav.addObject("roleHierarchy", roleHierarchyService.getById(Integer.parseInt(req.getParameter("id"))));
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("signList", getSignList());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.EDIT_ROLE_HIERARCHY,
        method = RequestMethod.POST
    )
    public ModelAndView editRole(@ModelAttribute("roleHierarchy") RoleHierarchy form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLE_HIERARCHY);

        if (form.getRole1() != form.getRole2()) {
            roleHierarchyService.update(form);
        }

        roleHierarchy.reloadHierarchy();

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.DELETE_ROLE_HIERARCHY,
        method = RequestMethod.POST
    )
    public ModelAndView deleteRoles(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLE_HIERARCHY);

        roleHierarchyService.deleteById(AppUtil.toInt(req.getParameterValues("ids")));
        roleHierarchy.reloadHierarchy();

        return mav;
    }
}



